package com.ruoyi.project.cms.product.controller;

import com.ruoyi.common.constant.ProductConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.product.domain.ProductSeries;
import com.ruoyi.project.cms.product.service.SeriesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreateTime 2020/5/17 16:59
 *  产品系列信息
 */
@Controller
@RequestMapping("/product/series")
public class ProductSeriesController extends BaseController {
    //指定一个全局地址前缀
    private String prefix = "/cms/product/series";

    @Autowired
    private SeriesService seriesService;

    //页面主体跳转
//    @RequiresPermissions("system:post:view") 权限过滤后期再处理
    @GetMapping("")
    public String series()
    {
        return prefix + "/series";
    }

    /**
     * 主页面菜单显示
     */
//    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductSeries series)
    {
        startPage();
        List<ProductSeries> list = seriesService.findAll(series);
        return getDataTable(list);
    }

    /**
     * 增加一个产品系列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 新增产品系列实现
     */
//    @RequiresPermissions("system:post:add")
//    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSeries(@Validated ProductSeries series)
    {
        if (ProductConstants.PRODUCT_SERIES_NAME_NOT_UNIQUE.equals(seriesService.checkSeriesNameUnique(series)))
        {
            return error("新增系列'" + series.getSeriesName() + "'失败，系列名称已存在");
        }
        return toAjax(seriesService.addSeries(series));
    }

    /**
     * 校验系列名称
     */
    @PostMapping("/checkSeriesNameUnique")
    @ResponseBody
    public String checkSeriesNameUnique(ProductSeries series)
    {
        return seriesService.checkSeriesNameUnique(series);
    }


    /**
     * 根据id查询
     *    数据回显
     */
    @GetMapping("/edit/{seriesId}")
    public String editSeries(@PathVariable("seriesId") Long seriesId, ModelMap mmap)
    {
        mmap.put("series",seriesService.findSeriesById(seriesId));
        return prefix + "/edit";
    }
    /**
     * 修改保存岗位
     */
//    @RequiresPermissions("system:post:edit")
//    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSaveSeries(@Validated ProductSeries series)
    {
        if (ProductConstants.PRODUCT_SERIES_NAME_NOT_UNIQUE.equals(seriesService.checkSeriesNameUnique(series)))
        {
            return error("修改系列'" + series.getSeriesName() + "'失败，系列名称已存在");
        }
        return toAjax(seriesService.changeSeries(series));
    }

    /**
     * 查看系列的具体信息
     */
//    @RequiresPermissions("monitor:operlog:detail")
    @GetMapping("/detail/{seriesId}")
    public String detailSeries(@PathVariable("seriesId") Long seriesId, ModelMap mmap)
    {
        mmap.put("series",seriesService.findSeriesById(seriesId));
        return prefix + "/detail";
    }

    /**
     * 删除系列信息
     */
//    @RequiresPermissions("system:post:remove")
//    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeSeries(String ids)
    {
        try
        {
            return toAjax(seriesService.removeSeriesByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
